package com.zxq.kingofbots.service.impl.user.bot;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxq.kingofbots.mapper.BotMapper;
import com.zxq.kingofbots.pojo.Bot;
import com.zxq.kingofbots.pojo.User;
import com.zxq.kingofbots.service.impl.utils.UserDetailsImpl;
import com.zxq.kingofbots.service.user.bot.RemoveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;

@Service
public class RemoveServiceImpl implements RemoveService {
    @Autowired
    private BotMapper botMapper;

    @Override
    public Map<String, String> removeBot(Map<String, String> data) {
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authentication.getPrincipal();
        User user = loginUser.getUser();

        Map<String,String> map=new HashMap<>();

        int id=Integer.parseInt(data.get("id"));
        QueryWrapper<Bot> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Bot bot = botMapper.selectById(id);

        if(bot==null){
            map.put("message","该bot不存在或已删除");
            return map;
        }
        if(!bot.getUserId().equals(user.getId())){
            map.put("message","非该bot的主人，不能删除该bot");
            return map;
        }

        botMapper.deleteById(bot);
        map.put("message","success");
        return map;
    }
}
